package business;

import com.shove.Convert;
import controllers.BackendLogin;
import models.sys_user;
import models.sys_user_role;
import play.Logger;
import play.cache.Cache;
import play.db.jpa.JPA;
import play.mvc.Scope;
import utils.Constants;

import java.util.Date;
import java.util.List;

public class SysUser extends BaseBiz {
    public static void setSessionUser(sys_user user){
        String sessionId = Scope.Session.current().getId();
        Cache.add(Constants.USERFIX+sessionId,user,"3h");
    }
    public static sys_user getSessionUser(){
        String sessionId = Scope.Session.current().getId();
        if(Cache.get(Constants.USERFIX+sessionId)!=null){
            sys_user user = Cache.get(Constants.USERFIX+sessionId, sys_user.class);
            return user;
        }
        return null;
    }
    public static sys_user login(Scope.Params params){
        String username = params.get("username");
        String password = params.get("password");
        List<sys_user> sys_userList = sys_user.find(" from sys_user a where a.username =? and a.password=? and status=1 ",username,password).fetch();
        if(sys_userList.size()==1)
        {
            return sys_userList.get(0);
        }else{
            return null;
        }
    }
    public static void checkLogin(){
        //调试默认登录用户,上线要关闭
        if(SysUser.getSessionUser()==null){
            BackendLogin.index();
        }
    }
    public static sys_user update(Scope.Params params){
        //添加
        sys_user sysUser;
        String id = params.get("id");
        if(id!=null && !id.equals("")){
            sysUser = sys_user.findById(Long.parseLong(id));
        }else{
            sysUser = new sys_user();
        }
        sysUser.username = params.get("username");
        sysUser.mobile = params.get("mobile");
        sysUser.password = params.get("password");
        sysUser.status =1;
        sysUser.common = params.get("common");
        sysUser.created_date = new Date();
        sysUser.save();
        //删除添加过的权限
        sys_user_role.delete(" from sys_user_role where user_id=?",sysUser.id);
        //重新添加权限表数据
        String roles = params.get("chkRoles");
        String[] roles2 = roles.split(",");
        for (int i=0;i<roles2.length ;i++){
            sys_user_role userRole = new sys_user_role();
            userRole.role_id = Convert.strToLong(roles2[i],0);
            userRole.user_id = Convert.strToLong(sysUser.id+"",0);
            userRole.save();
        }
        return sysUser;
    }
    public static boolean isAdmin(){
        sys_user sysUser = getSessionUser();
        if(sysUser==null){
            Logger.info("isHavePermission.isnull");
            return false;
        }
        if(sysUser.username.equals("admin")){
            return true;
        }
        return false;
    }
    public static boolean isHavePermission(String url){
        sys_user sysUser = getSessionUser();
        boolean bResult = isAdmin();
        String sql = "select c.id,c.menu_name,c.access_url from sys_menu c where c.id in ( select b.menu_id from sys_role_menu b where b.role_id in (  select a.role_id from sys_user_role a where a.user_id = ? )  )";
        List list = JPA.em().createNativeQuery(sql).setParameter(1,sysUser.id).getResultList();
        if(list!=null){
            for (int i=0;i<list.size();i++){
                Object[] objects = (Object[]) list.get(i);
                String access_url = objects[2].toString();
                if(url.contains(access_url)){
                    bResult = true;
                    break;
                }
            }
        }else{
            Logger.info("list is null");
        }
        return bResult;
    }
}
